package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.customerstatement.pojo.dto.StatementCustomerListDTO;
import com.xbongbong.pro.customerstatement.pojo.dto.StatementCustomerSummaryDTO;
import com.xbongbong.pro.customerstatement.pojo.dto.StatementCustomerTableDTO;
import com.xbongbong.pro.customerstatement.pojo.dto.StatementCustomerTotalDTO;
import com.xbongbong.pro.customerstatement.pojo.vo.StatementCustomerListVO;
import com.xbongbong.pro.customerstatement.pojo.vo.StatementCustomerSummaryVO;
import com.xbongbong.pro.customerstatement.pojo.vo.StatementCustomerTableVO;
import com.xbongbong.pro.customerstatement.pojo.vo.StatementCustomerTotalVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.service.CustomerStatementService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/customerStatement")
public class CustomerStatementController {
    @Resource
    private CustomerStatementService customerStatementService;

    /**
     * 获取客户对账单外层列表
     * @param formDataListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/parentList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String contractList(@RequestBody @Valid FormDataListDTO formDataListDTO, BindingResult br) {
        XbbResponse<PaasFormDataEsListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(formDataListDTO.getSaasMark(), formDataListDTO.getBusinessType(), formDataListDTO.getMenuId(), formDataListDTO.getDistributorMark());
                PaasFormDataEsListVO formDataListVO = customerStatementService.parentList(formDataListDTO);
                response = new XbbResponse<>(formDataListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 对账单外层客户id、name列表
     * @param statementCustomerListDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/customer/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String customerList(@RequestBody @Valid StatementCustomerListDTO statementCustomerListDTO, BindingResult br) {
        XbbResponse<StatementCustomerListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StatementCustomerListVO statementCustomerListVO = customerStatementService.customerIdList(statementCustomerListDTO);
                response = new XbbResponse<>(statementCustomerListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 对账单_内层对账表(单个客户)
     * @param statementCustomerTableDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/customer/table", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String customerTable(@RequestBody @Valid StatementCustomerTableDTO statementCustomerTableDTO, BindingResult br) {
        XbbResponse<StatementCustomerTableVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StatementCustomerTableVO statementCustomerTableVO = customerStatementService.customerTable(statementCustomerTableDTO);
                response = new XbbResponse<>(statementCustomerTableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 对账单_单个客户_合计/剩余
     * @param statementCustomerSummaryDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/customer/summary", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String customerSummary(@RequestBody @Valid StatementCustomerSummaryDTO statementCustomerSummaryDTO, BindingResult br) {
        XbbResponse<StatementCustomerSummaryVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StatementCustomerSummaryVO statementCustomerTableVO = customerStatementService.customerSummary(statementCustomerSummaryDTO);
                response = new XbbResponse<>(statementCustomerTableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 对账单_单个客户_合计/剩余
     * @param statementCustomerTotalDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/customer/total", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String customerTotal(@RequestBody @Valid StatementCustomerTotalDTO statementCustomerTotalDTO, BindingResult br) {
        XbbResponse<StatementCustomerTotalVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StatementCustomerTotalVO statementCustomerTotalVO = customerStatementService.customerTotal(statementCustomerTotalDTO);
                response = new XbbResponse<>(statementCustomerTotalVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }



}
